package com.uchiha.nova.mainService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaDefaultConstant;
import com.uchiha.nova.core.tool.constant.NovaRoleConstant;
import com.uchiha.nova.core.model.dto.TenantDTO;
import com.uchiha.nova.core.model.dto.TenantUserDTO;
import com.uchiha.nova.core.model.entity.RoleEntity;
import com.uchiha.nova.core.model.entity.TenantUserEntity;
import com.uchiha.nova.core.model.entity.UserEntity;
import com.uchiha.nova.core.model.entity.UserRoleEntity;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import com.uchiha.nova.core.tool.util.BeanUtil;
import com.uchiha.nova.mainService.mapper.TenantUserMapper;
import com.uchiha.nova.mainService.service.*;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 租户用户管理 服务实现类
 *
 * @author Chill
 */
@Service
public class TenantUserServiceImpl extends BaseServiceImpl<TenantUserMapper, TenantUserEntity> implements ITenantUserService {
    @Resource
    UserRoleService userRoleService;
    @Resource
    UserService userService;
    @Resource
    IRoleService roleService;
    @Resource
    INavMenuService navMenuService;



    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initTenant(TenantDTO tenantDTO) {

        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(tenantDTO.getTenant());
        userEntity.setPassword(NovaDefaultConstant.DEFAULT_PASSWORD);
        userEntity.setAvatar(NovaDefaultConstant.DEFAULT_AVATAR);
        userEntity.setName(tenantDTO.getTenantName());
        userEntity.setTenantId(tenantDTO.getTenant());

        userService.save(userEntity);
        Long userId = userEntity.getId();
        RoleEntity roleEntity = roleService.getOne(new QueryWrapper<RoleEntity>().eq("role_name", NovaRoleConstant.ROLE_Tenant_Admin));
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setUserId(userId);
        userRoleEntity.setRoleId(roleEntity.getId());
        userRoleService.save(userRoleEntity);
        TenantUserEntity tenantUser = new TenantUserEntity();
        tenantUser.setId(userId);
        tenantUser.setTenant(tenantDTO.getTenant());
        tenantUser.setUserCompanyId(1L);
        tenantUser.setUserDeptId(1L);
        tenantUser.setUuid(tenantDTO.getTenant());
        tenantUser.setImg(userEntity.getAvatar());
        tenantUser.setName(tenantDTO.getTenantName());
        tenantUser.setTenantId(tenantDTO.getTenant());
        tenantUser.setUserPostId(1L);
        tenantUser.setDeptId(1L);
        tenantUser.setCompanyId(1L);
        tenantUser.setPostId(1L);
        tenantUser.setStatus(1);
        this.save(tenantUser);
        navMenuService.initTenant(tenantDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveInit(TenantUserDTO tenantUserDTO) {
        Long a = this.count(new QueryWrapper<TenantUserEntity>().eq("uuid",tenantUserDTO.getUuid()));
        if(a.compareTo(0L)>0){
            return R.fail("重复的用户名");
        }
        TenantUserEntity tenantUserEntity = new TenantUserEntity();
        BeanUtils.copyProperties(tenantUserDTO, tenantUserEntity);
        tenantUserEntity.setTenantId(ObjectUtil.isNotEmpty(tenantUserDTO.getTenant())?tenantUserDTO.getTenant():"1111");
        tenantUserEntity.setCompanyId(ObjectUtil.isNotEmpty(tenantUserDTO.getCompanyId())?tenantUserDTO.getCompanyId():1L);
        tenantUserEntity.setDeptId(ObjectUtil.isNotEmpty(tenantUserDTO.getDeptId())?tenantUserDTO.getDeptId():1L);
        tenantUserEntity.setPostId(ObjectUtil.isNotEmpty(tenantUserDTO.getPostId())?tenantUserDTO.getPostId():1L);
        Long roleId = tenantUserDTO.getRoleId();
        UserEntity userEntity = new UserEntity();
        userEntity.setAvatar(ObjectUtil.isNotEmpty(tenantUserDTO.getImg())?tenantUserDTO.getImg():NovaDefaultConstant.DEFAULT_AVATAR);
        userEntity.setPassword(NovaDefaultConstant.DEFAULT_PASSWORD);
        userEntity.setName(tenantUserDTO.getName());
        userEntity.setUsername(tenantUserDTO.getUuid());
        userEntity.setMobile(tenantUserDTO.getMobile());
        userEntity.setTenantId("1111");
        userEntity.setIdentityCard(ObjectUtil.isNotEmpty(tenantUserDTO.getIdentityCard())?tenantUserDTO.getIdentityCard():"1");
        userEntity.setNickName(ObjectUtil.isNotEmpty(tenantUserDTO.getNickName())?tenantUserDTO.getNickName():null);
        userEntity.setCompanyId(tenantUserEntity.getUserCompanyId());
        userEntity.setDeptId(tenantUserEntity.getUserDeptId());
        userEntity.setPostId(tenantUserEntity.getUserPostId());
        userService.save(userEntity);
        Long userId = userEntity.getId();
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setUserId(userId);
        userRoleEntity.setRoleId(roleId);
        userRoleService.save(userRoleEntity);

        tenantUserEntity.setId(userId);
        this.save(tenantUserEntity);
        return R.success("添加成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateTenantUser(TenantUserDTO tenantUserDTO) {
        TenantUserEntity entityEx = this.getOne(new QueryWrapper<TenantUserEntity>().eq("uuid",tenantUserDTO.getUuid()));
        if(ObjectUtil.isNotEmpty(entityEx)){
            if(entityEx.getId().compareTo(tenantUserDTO.getId())!=0){
                return R.fail("重复的用户名");
            }
        }

        TenantUserEntity tenantUserEntity = new TenantUserEntity();
        BeanUtils.copyProperties(tenantUserDTO, tenantUserEntity);
        tenantUserEntity.setTenantId(ObjectUtil.isNotEmpty(tenantUserDTO.getTenant())?tenantUserDTO.getTenant():"1111");
        tenantUserEntity.setCompanyId(ObjectUtil.isNotEmpty(tenantUserDTO.getCompanyId())?tenantUserDTO.getCompanyId():1L);
        tenantUserEntity.setDeptId(ObjectUtil.isNotEmpty(tenantUserDTO.getDeptId())?tenantUserDTO.getDeptId():1L);
        tenantUserEntity.setPostId(ObjectUtil.isNotEmpty(tenantUserDTO.getPostId())?tenantUserDTO.getPostId():1L);
        this.updateById(tenantUserEntity);
        Long roleId = tenantUserDTO.getRoleId();
        userRoleService.deleteUser(tenantUserEntity.getId());
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setUserId(tenantUserEntity.getId());
        userRoleEntity.setRoleId(roleId);
        userRoleService.save(userRoleEntity);
        UserEntity userEntity = userService.getById(tenantUserEntity.getId());
        userEntity.setAvatar(ObjectUtil.isNotEmpty(tenantUserDTO.getImg())?tenantUserDTO.getImg():NovaDefaultConstant.DEFAULT_AVATAR);
        userEntity.setName(tenantUserDTO.getName());
        userEntity.setUsername(tenantUserDTO.getUuid());
        userEntity.setMobile(tenantUserDTO.getMobile());
        userService.updateById(userEntity);
        return R.success("修改成功");
    }


}
